package cl.portal.escolares.json;  
      
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cl.portal.escolares.DAO.DAOCurso;
import cl.portal.escolares.domain.Curso;
      
    public class JSONColegioCurso extends HttpServlet  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		public void init( ServletConfig config ) throws ServletException  
        {  
        }  
        public final void doGet( HttpServletRequest request, HttpServletResponse response )  
            throws ServletException, IOException  
        {    
            try  
            {  
            	Long colegioId=Long.parseLong(request.getParameter("parentId"));
        		List<Curso> CursoList = new ArrayList<Curso>();
    			System.out.println("colegioId"+colegioId);
        		CursoList=DAOCurso.list(colegioId, true);
        		JSONArray jsa = new JSONArray();
        		for(Curso c:CursoList){
        			JSONObject jso = new JSONObject();
        			jso.put("label", c.getNombre());
        			jso.put("value", c.getCursoId());
        			jsa.add(jso);
        		}
    			System.out.println(jsa.toString());

        		response.setContentType("text/html");
        		PrintWriter writer=response.getWriter();
        		writer.write(jsa.toString());
        		writer.close();
            }  
            catch(Exception ex)  
            {  
            }  
        }  
    }  
